package me.helllp.book.concurrent.ch03;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 输出线程池错误堆栈
 * 
 * @author Administrator
 *
 */
public class TraceThreadPoolExecutor extends ThreadPoolExecutor{
	public TraceThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
			long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue){
		super(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue);
	}
	
	@Override
	public void execute(Runnable command) {
		super.execute(wrap(command, clientTrace(),Thread.currentThread().getName()));
	}
	
	@Override
	public Future<?> submit(Runnable task) {
		return super.submit(wrap(task, clientTrace(), Thread.currentThread().getName()));
	}

	
	private Exception clientTrace(){
		return new Exception("客户端堆栈信息");
	}
	
	private Runnable wrap(final Runnable task, final Exception clientSstack, String clientThreadName){
		return new Runnable() {
			public void run() {
				try {
					task.run();
				} catch (Exception e) {
					clientSstack.printStackTrace();
					throw e;
				}
			}
		};
	}
	
	static class DivTask implements Runnable{
		int a,b;
		public DivTask(int a, int b){
			this.a = a;
			this.b = b;
		}
		
		@Override
		public void run() {
			double c = a/b;
			System.out.println(c);
		}
	}
	
	public static void main(String[] args) {
		ThreadPoolExecutor pools = new TraceThreadPoolExecutor(0, Integer.MAX_VALUE, 0L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
		
		for(int i=0; i<5; i++){
			pools.execute(new DivTask(100, i));
		}
	}
}
